var gulp         = require('gulp')
var render       = require('gulp-nunjucks-render')
var rename       = require('gulp-rename')
var handleErrors = require('../../lib/handleErrors')
var log          = require('fancy-log')
var colors       = require('ansi-colors')
var data         = require('gulp-data')

module.exports = function(config) {
  return function(glyphs, options) {
    log(colors.blue('Generating ' + config.sassDest + '/' + config.sassOutputName))
    render.nunjucks.configure(config.nunjucks, { watch: false })

    return gulp.src(config.template)
      .pipe(data({
        icons: glyphs.map(function(glyph) {
          log(colors.green('+ adding ' + glyph.name + ' glyph'))
          return {
            name: glyph.name,
            code: glyph.unicode[0].charCodeAt(0).toString(16).toUpperCase()
          }
        }),

        fontName: config.options.fontName,
        fontPath: config.fontPath,
        className: config.className,
        comment: '// DO NOT EDIT DIRECTLY!\n  //Generated by gulpfile.js/tasks/iconFont.js\n  //from ' + config.template
      }))
      .pipe(render({
        path: config.template
      }))
    .on('error', handleErrors)
    .pipe(rename(config.sassOutputName))
    .pipe(gulp.dest(config.sassDest))
  }
}
